package Homework;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class work02 {
    public static void main(String[] args) throws Exception {
        FileInputStream fileReader = new FileInputStream("E:\\大三课件（上）\\java学习2\\课件\\决策引擎Java-data.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileReader,"utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String line=null;
        StringBuffer strbu = new StringBuffer();
        while((line=bufferedReader.readLine())!=null){
            System.out.println(line);
            strbu.append(line+"\n");
        }
        bufferedReader.close();

        String str = strbu.toString();
        String[] str2 = str.split("\n");
        String hour = str2[1];
        String half = str2[3];
        String quarter = str2[5];

        HashMap<String,Integer> map1 = method1(hour,half);
        System.out.println("半小时数据：");
        ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(map1.entrySet());
        list.stream().forEach(s -> System.out.print("<" + s.getKey() + "," + s.getValue() + "> "));
        System.out.println("\n"+"半小时排序数据：");
        sort(map1);

        HashMap<String,Integer> map2 = method2(hour,quarter);
        System.out.println("\n"+"15分钟数据：");
        ArrayList<Map.Entry<String, Integer>> list2 = new ArrayList<>(map2.entrySet());
        list2.stream().forEach(s -> System.out.print("<" + s.getKey() + "," + s.getValue() + "> "));
        System.out.println("\n"+"15分钟排序数据：");
        sort(map2);
    }

    //获取数据并进行拼接
    public static HashMap<String,Integer> method1(String hour,String half){
        HashMap<String, Integer> hashMap = new HashMap<>();
        String[] strHour = hour.split(";");
        String[] strHalf = half.split(";");

        for(int i=0;i<strHour.length;i++){
            String[] str = strHalf[i].split(",");
            for(int j = 0; j < str.length; j++){//注意数组越界异常(ArrayIndexOutOfBoundsException)
                if(j==0){
                    hashMap.put("0"+strHour[i]+"30",Integer.parseInt(str[j].split(":")[1]));
                }else {
                    hashMap.put("0"+strHour[i]+"60",Integer.parseInt(str[j].split(":")[1]));
                }
            }
        }

        return hashMap;
    }

    //对HashMap中的值进行从大到小的排序并打印
    public static HashMap<String,Integer> method2(String hour,String quarter){
        HashMap<String, Integer> hashMap = new HashMap<>();
        String[] strHour = hour.split(";");
        String[] strQuarter = quarter.split(";");

        for(int i=0;i<strHour.length;i++){
            String[] str = strQuarter[i].split(",");
            for(int j = 0; j < str.length; j++){//注意数组越界异常(ArrayIndexOutOfBoundsException)
                if(j==0){
                    hashMap.put("0"+strHour[i]+"15",Integer.parseInt(str[j].split(":")[1]));
                }else if(j==1) {
                    hashMap.put("0"+strHour[i]+"30",Integer.parseInt(str[j].split(":")[1]));
                }else if(j==2) {
                    hashMap.put("0"+strHour[i]+"45",Integer.parseInt(str[j].split(":")[1]));
                }else {
                    hashMap.put("0"+strHour[i]+"60",Integer.parseInt(str[j].split(":")[1]));
                }
            }
        }
        return hashMap;
    }

    public static void sort(HashMap<String,Integer> map){
        ArrayList<Map.Entry<String, Integer>> list=new ArrayList<>(map.entrySet());
        list.stream().sorted(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue()-o1.getValue();
            }
        }).forEach(s-> System.out.print("<"+s.getKey()+","+s.getValue()+"> "));
    }
}
